import NProgress from "nprogress";

import Vue from 'vue'
import VueRouter from 'vue-router'

import LoginPage from "@/views/LoginPage.vue"

Vue.use(VueRouter)

const routes = [
	{
		path:"/",
		redirect:"/login"
	},
	{
		path: "/login",
		component: LoginPage,
		meta:{
			public:true
		}
	},
	{
		path: '/home',
		name: 'home',
		// 路由懒加载 - 程序在打包时，单独将该路由文件进行文件生成，并在项目运行时，更具需要完成异步加载
		// 以此减小 app.js 文件的大小，优化程序的启动速度
		component: () => import(/* webpackChunkName: "pages" */"@/views/HomePage.vue"),
		redirect:"/welcome", // 在父子路由情况下，子组件path为绝对路径时，通过该方式指定子路由的默认访问
		children:[
			{
				path:"/welcome",
				component:() => import(/* webpackChunkName: "pages" */"@/views/welcome/WelcomePage.vue"),
				meta:{
					title:["首页","欢迎页"]
				}
			},
			{
				path:"/users",
				component:() => import(/* webpackChunkName: "pages" */"@/views/users/UserList.vue"),
				meta:{
					title:["首页","用户管理","用户列表"]
				}
			},
			{
				path:"/roles",
				component:() => import(/* webpackChunkName: "pages" */"@/views/roles/RolesPage.vue"),
				meta:{
					title:["首页","权限管理","角色列表"]
				}
			},
			{
				path:"/rights",
				component:() => import(/* webpackChunkName: "pages" */"@/views/rights/RightsList.vue"),
				meta:{
					title:["首页","权限管理","权限列表"]
				}
			},
			{
				path:"/goods",
				component:() => import(/* webpackChunkName: "pages" */"@/views/goods/GoodsPage.vue"),
				meta:{
					title:["首页","商品管理","商品列表"]
				}
			},
			{
				path:"/goods/add",
				component:() => import(/* webpackChunkName: "pages" */"@/views/goods/AddGoods.vue"),
				meta:{
					title:["首页","商品管理","商品添加"]
				}
			},
			{
				path:"/goods/set/:id",
				component:() => import(/* webpackChunkName: "pages" */"@/views/goods/SetGoods.vue"),
				props:true,
				meta:{
					title:["首页","商品管理","商品修改"]
				}
			},
		]
	},
	{
		path:"*",
		component:() => import(/* webpackChunkName: "pages" */"@/views/NotFound.vue"),
	}
]

const router = new VueRouter({
	routes
})

router.beforeEach((to,from,next)=>{
	NProgress.start();
	// console.log(to);
	if(to.meta.public){
		return next();
	}
	let user = sessionStorage.getItem("user");
	if(user==null){
		return next("/login");
	}
	// console.log(to.path)
	// if(to.path=="/users"){
	// 	alert("没有权限访问该页面")
	// }else{
	// 	next();
	// }
	next();
});
router.afterEach(()=>{
	NProgress.done();
})

export default router
